
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  data: {
    payType: "",
    orderNumber: "",
    sheetFlag: true,
    canM: "",
    M: "",
    fee: "",
    MFee: "",
    phoneNum: "",
    sheetArr: ['5000', '10000', '15000', '20000', '25000', '30000', ],
    radioArr: [{
        img: "../../images/pay0.png",
        words: "余额",
        value: 1,
      },
      {
        img: "../../images/pay1.png",
        words: "微信",
        value: 2,
      }
    ],
    // 
    uid: "",
    userType: "",
    // 输入支付密码
    //是否展示密码输入层
    showPayPwdInput: false,
    //输入的密码
    payPasswords: "",
    //文本框焦点
    payFocus: true,
    // 是否已经设置支付密码
    isAlreadyPayPassword: "",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      isAlreadyPayPassword: wx.getStorageSync('isAlreadyPasswords'),
    })
    this.GetSJAccount();
  },

  //显示支付密码输入层
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: true,
      payFocus: true
    });
  },
  //隐藏支付密码输入层
  cancelInputPasswords: function() {
    var val = this.data.payPasswords;
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      payPasswords: ''
    });

  },
  // 获取焦点
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  //输入密码,监听
  inputPwd: function(e) {
    this.setData({
      payPasswords: e.detail.value
    });
  },
  //输入密码 确定 
  surePasswords: function() {
    var that = this;
    var uid = this.data.uid;
    var payPasswords = this.data.payPasswords;
    var orderNumber = this.data.orderNumber;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CheckMyTradePassword&UID=" + uid + "&TradePassword=" + payPasswords),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl("method=BorrowMaYuePay&orderNumber=" + orderNumber),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              console.log(json);
              if (json.ResultFlag == 1) {
                wx.navigateBack({
                  delta: 1
                })

              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  // GetSJAccount
  GetSJAccount: function() {
    var that = this;
    var uid = this.data.uid;
    var canM = this.data.canM;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetSJAccount&SJID=" + uid),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        console.log(json);
        canM = parseInt(json.GradeMaxBorrowMa) - parseInt(json.nowGradeBorrowMa);
        that.setData({
          canM: canM,
          fee: parseFloat(json.BorrowMaFee),
          phoneNum: json.OperaterPhone
        })

      }
    })

  },
  //sheetTap
  sheetTap: function() {
    this.setData({
      sheetFlag: !this.data.sheetFlag
    })
  },
  // chooseM
  chooseM: function(e) {
    var that = this;
    var M = this.data.M;
    M = e.currentTarget.dataset.value
    var fee = this.data.fee;
    var MFee = this.data.MFee;
    var canM = this.data.canM;
    MFee = (M * fee) / 100;
    if (M > canM) {
      wx.showToast({
        title: "可借额度不足！",
        icon: 'none',
        duration: 2000
      });
    } else {
      this.setData({
        sheetFlag: !this.data.sheetFlag,
        M: M,
        MFee: MFee,
      })
    }
  },
  // radioChange余额，微信
  radioChange: function(e) {
    console.log(e);
    this.setData({
      payType: e.detail.value,
    })

  },
  // pay支付
  pay: function() {
    var that = this;
    var uid = this.data.uid;
    var M = this.data.M;
    var MFee = this.data.MFee;
    var phoneNum = this.data.phoneNum;
    var payType = this.data.payType;
    var orderNumber = this.data.orderNumber;
    var isAlreadyPayPassword = this.data.isAlreadyPayPassword;
    if (payType == '') {
      wx.showToast({
        title: "请选择支付方式！",
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=CreareBorrowMaOrder&UID=" + uid + "&BorrowMa=" + M + "&BorroqMaFee=" + MFee + "&Phone=" + phoneNum),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        console.log(json);
        if (json.ResultFlag == 1) {
          orderNumber = json.ResultData;
          that.setData({
            orderNumber: orderNumber,
          })
          if (payType == 1) {
            if (isAlreadyPayPassword == '1') {
              that.setData({
                showPayPwdInput: true,
                payFocus: true
              })
            } else {
              wx.showToast({
                title: "请先去设置支付密码！",
                icon: 'none',
                duration: 2000
              });
            }

          } else {
            // 微信
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=BorrowMaWeixinPay&orderNumber=" + orderNumber),
              success: function(res) {
                var json = JSON.parse(baseJs.decryUrl(res.data));
                console.log(json);
                if (json.ResultFlag == 1) {
                  wx.requestPayment({
                    timeStamp: json.ResultData.timestamp,
                    nonceStr: json.ResultData.nonce_str,
                    package: 'prepay_id=' + json.ResultData.prepay_id,
                    signType: 'MD5',
                    paySign: json.ResultData.sign,
                    success(res) {
                      console.log(res);
                    },
                    fail(res) {
                      console.log(res);
                      wx.showToast({
                        title: res.err_desc,
                        icon: 'none',
                        duration: 2000
                      });
                    }
                  })
                } else {
                  wx.showToast({
                    title: json.ResultMsg,
                    icon: 'none',
                    duration: 2000
                  });
                }
              }
            })
          }
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
  //
})